package com.woniuxy.kaer.service.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.io.FileUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.kaer.commons.util.Data;
import com.woniuxy.kaer.commons.util.IKAnalyzerUtil;
import com.woniuxy.kaer.commons.util.OSSUtil;
import com.woniuxy.kaer.commons.util.Result;
import com.woniuxy.kaer.service.dto.ConsumablesDto;
import com.woniuxy.kaer.service.dto.ServiceBaseDto;
import com.woniuxy.kaer.service.dto.ServiceProjectDto;
import com.woniuxy.kaer.service.entity.Consumables;
import com.woniuxy.kaer.service.entity.ServiceBase;
import com.woniuxy.kaer.service.entity.ServiceProject;
import com.woniuxy.kaer.service.mapper.ConsumablesMapper;
import com.woniuxy.kaer.service.mapper.ServiceBaseMapper;
import com.woniuxy.kaer.service.mapper.ServiceProjectMapper;
import com.woniuxy.kaer.service.param.AddServiceBaseParam;
import com.woniuxy.kaer.service.service.ServiceBaseService;
import com.woniuxy.kaer.tenants.dto.ShopInfoDto;
import com.woniuxy.kaer.tenants.feign.TenantsFeignClient;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 程诗洋
 * @since 2025年02月17日
 */
@Service
public class ServiceBaseServiceImpl extends ServiceImpl<ServiceBaseMapper, ServiceBase> implements ServiceBaseService {

    @Resource
    private ServiceBaseMapper serviceBaseMapper;
    @Resource
    private TenantsFeignClient tenantsFeignClient;
    @Resource
    private ServiceProjectMapper serviceProjectMapper;
    @Resource
    private ConsumablesMapper consumablesMapper;
    @Resource
    private OSSUtil ossUtil;
    @Override
    public void AddServiceBase(AddServiceBaseParam param) throws Exception {
        Result<ShopInfoDto> result = tenantsFeignClient.getShopInfoByShopId(param.getShopId());
        ShopInfoDto resultData = result.getData();
        if(resultData==null){
            throw new Exception("店铺不存在");
        }
        byte[] bytes = new byte[4];
        param.getServiceBasePicture().getInputStream().read(bytes);
        if(!(ossUtil.isJpg(bytes)||ossUtil.isPng(bytes))){
            throw new Exception("文件格式不正确");
        }
        String s = FileUtil.extName(param.getServiceBasePicture().getOriginalFilename());
        String s1 = UUID.randomUUID().toString();
        String serviceBasePicture = ossUtil.uploadFile(param.getServiceBasePicture().getInputStream(), s1 + "." + s, "guojiandong");
        ServiceBase serviceBase = BeanUtil.toBean(param, ServiceBase.class);
        serviceBase.setServiceBasePicture(serviceBasePicture);
        serviceBase.setCreateTime(LocalDateTime.now());
        serviceBase.setServiceBaseState(Data.SERVICE_BASE_STATE_NORMAL);
        serviceBaseMapper.insert(serviceBase);
    }

    @Override
    public List<Integer> getServiceBaseByServiceName(String serviceName) throws Exception {
        List<String> keywords = IKAnalyzerUtil.analyze(serviceName);
        List<Integer> ShopIds = new ArrayList<>();
        QueryWrapper<ServiceBase> serviceBaseQueryWrapper = new QueryWrapper<>();
        for (String keyword : keywords) {
            serviceBaseQueryWrapper.or().like("service_base_name",keyword);
        }
        List<ServiceBase> serviceBases = serviceBaseMapper.selectList(serviceBaseQueryWrapper);
        for (ServiceBase serviceBase : serviceBases) {
            ShopIds.add(serviceBase.getShopId());
        }
        return ShopIds;
    }

    @Override
    public List<ServiceBase> getServiceBaseByShopId(Integer shopId) throws Exception {
        QueryWrapper<ServiceBase> serviceBaseQueryWrapper = new QueryWrapper<>();
        serviceBaseQueryWrapper.eq("shop_id",shopId);
        List<ServiceBase> serviceBases = serviceBaseMapper.selectList(serviceBaseQueryWrapper);
        return serviceBases;
    }

    @Override
    public List<ServiceBaseDto> getServiceBaseByShopIdAndServiceName(Integer shopId) throws Exception {
        ArrayList<ServiceBaseDto> serviceBaseDtos = new ArrayList<>();
        QueryWrapper<ServiceBase> serviceBaseQueryWrapper = new QueryWrapper<>();
        serviceBaseQueryWrapper.eq("shop_id",shopId);
        List<ServiceBase> serviceBases = serviceBaseMapper.selectList(serviceBaseQueryWrapper);
        for (ServiceBase serviceBase : serviceBases) {
            ServiceBaseDto serviceBaseDto = BeanUtil.toBean(serviceBase, ServiceBaseDto.class);
            QueryWrapper<ServiceProject> serviceProjectQueryWrapper = new QueryWrapper<>();
            serviceProjectQueryWrapper.eq("service_base_id",serviceBase.getServiceBaseId());
            List<ServiceProject> serviceProjects = serviceProjectMapper.selectList(serviceProjectQueryWrapper);
            for (ServiceProject serviceProject : serviceProjects) {
                ServiceProjectDto serviceProjectDto = BeanUtil.toBean(serviceProject, ServiceProjectDto.class);
                QueryWrapper<Consumables> consumablesQueryWrapper = new QueryWrapper<>();
                consumablesQueryWrapper.eq("service_project_id",serviceProject.getServiceProjectId());
                List<Consumables> consumables = consumablesMapper.selectList(consumablesQueryWrapper);
                for (Consumables consumable : consumables) {
                    serviceProjectDto.getConsumablesDtoList().add(BeanUtil.toBean(consumable, ConsumablesDto.class));
                }
                if(null!=serviceBaseDto&&null!=serviceBaseDto.getServiceProjectDtoList()){
                    serviceBaseDto.getServiceProjectDtoList().add(serviceProjectDto);
                }
            }
            serviceBaseDtos.add(serviceBaseDto);
        }

        return serviceBaseDtos;
    }


}
